import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:life_milestones/MS-global-url.dart';
import 'package:life_milestones/MS-request.dart';
import 'package:life_milestones/controller/MS-global-user-controller.dart';
import 'package:app_tracking_transparency/app_tracking_transparency.dart';
class MSLaunchPage extends StatefulWidget{
  const MSLaunchPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _MSLaunchPageState();
  }
  
}

class _MSLaunchPageState extends State<MSLaunchPage>{
  Timer? timer;
  initAppTrack()async{
    final TrackingStatus status = await AppTrackingTransparency.trackingAuthorizationStatus;
    if (status == TrackingStatus.notDetermined) {
      Future.delayed(const Duration(seconds: 2), () async{
        await AppTrackingTransparency.requestTrackingAuthorization();
      });

    } else {
      timer?.cancel();
    }
  }
  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(const Duration(milliseconds: 2000),(timer){
      initAppTrack();
    });
  WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
    if(MSGlobalUserController().token != null){
      MSRequest().post(MSGlobalUrl.verifyToken,data: {"token":MSGlobalUserController().token}).then((value)async{
        if(value["code"] == 0){
          MSGlobalUserController().refreshUserInfo().then((value){
            Navigator.of(context).pushReplacementNamed("/");
          });
        }else{
          MSGlobalUserController().token = null;
          Navigator.of(context).pushReplacementNamed("login");
        }
      });
    }else{
      Navigator.of(context).pushReplacementNamed("login");
    }
  });

  }
  @override
  Widget build(BuildContext context) {
   return Scaffold(
     body:SizedBox.expand(
       child: Stack(children: [
         Positioned.fill(child: 
         Image.asset("images/launch.png",fit: BoxFit.contain,)
         ),
         Positioned(bottom: 167.w,left: 0,right: 0,child:  Text("LifeMilestones",style:  TextStyle(fontSize: 40.sp,fontWeight: FontWeight.w900,color: Colors.black),textAlign: TextAlign.center,),),
         Positioned(bottom: 133.w,left: 0,right: 0,child:  Text("Record your every day",style:  TextStyle(fontSize: 15.sp,fontWeight: FontWeight.w900,color: Colors.black),textAlign: TextAlign.center,),)
       ],),
     )
   );
  }
  
}